﻿namespace Dissemination.MVC.Controllers
{
    using System.Web.Mvc;
    using Repositories;
    using ViewModels;
    using Models;
    using System;

    [HandleError]
    public class ParticipantController : ControllerBase
    {
        protected ParticipantRepository Repository = new ParticipantRepository();

        #region Details

        //
        // GET: /Participant/Details/5

        /// <summary>
        /// Returns the view associated to the details of an entity in the data source.
        /// </summary>
        /// <param name="id">The entity id.</param>
        /// <returns>Details view</returns>
        [Authorize(Roles = "Demostrator,Administrator,Reporter")]
        public ActionResult Details(int id)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Participant details with id={0}...", id);
            Participant participant = Repository.Get(id);
            
            if (participant == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Participant with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Participant details with id={0}...", id);
            return View(participant);
        }

        #endregion

        #region New

        //
        // GET: /Participant/Create

        /// <summary>
        /// Creates the specified entity.
        /// </summary>
        /// <param name="seminarID">The entity</param>
        /// <returns>Details view</returns>
        [Authorize(Roles = "Administrator,Reporter")]
        public ActionResult Create(int seminarID)
        {
            if (log.IsDebugEnabled) log.Debug("Entering Participant creation (POST)..."); 
            
            var viewModel = new ParticipantManagerViewModel
            {
                Participant = new Participant
                {
                    Seminar = new Seminar
                    {
                        SeminarID = seminarID
                    }
                },
                Genders = Repository.ListGenders(),
                ProfessionalBackgrounds = Repository.ListProfessionalBackgrounds(),
                Specialities = Repository.ListSpecialities()
            };

            if (log.IsDebugEnabled) log.Debug("Leaving Participant creation (POST)..."); 
            return View(viewModel);
        } 

        //
        // POST: /Participant/Create

        /// <summary>
        /// Creates the specified entity.
        /// </summary>
        /// <param name="Participant">The entity</param>
        /// <returns>Details view</returns>
        [HttpPost]
        [Authorize(Roles = "Administrator,Reporter")]
        public ActionResult Create(Participant participant)
        {
            if (log.IsDebugEnabled) log.Debug("Entering Participant creation (POST)...");
            int seminarID = int.Parse(RouteData.Values["seminarID"].ToString());
            participant.Seminar = Repository.GetSeminarByID(seminarID);
            participant.Gender = Repository.GetGenderByID(int.Parse(Request["Participant.ParticipantGenderID"]));
            participant.Speciality = Repository.GetSpecialityByID(int.Parse(Request["Participant.ParticipantSpecialityID"]));
            participant.ProfessionalBackground = Repository.GetProfessionalBackgroundByID(int.Parse(Request["Participant.ParticipantProfessionalBackgroundID"]));

            if (ModelState.IsValid)
            {
                Repository.Add(participant);
                Repository.Save();

                if (log.IsDebugEnabled) log.Debug("Leaving Participant creation (POST)...");
                return RedirectToAction("Details", "Seminar", new { id = seminarID });
            }

            var viewModel = new ParticipantManagerViewModel
            {
                Participant = participant,
                Genders = Repository.ListGenders(),
                ProfessionalBackgrounds = Repository.ListProfessionalBackgrounds(),
                Specialities = Repository.ListSpecialities()
            };

            if (log.IsDebugEnabled)
            {
                //TODO: Create_Participant
            }

            return View(viewModel);
        }

        #endregion

        #region Edit

        //
        // GET: /TargetArea/Edit/5

        /// <summary>
        /// Returns the view associated with the edit of an existing entity.
        /// </summary>
        /// <param name="id">The id of the entity to edit</param>
        /// <returns>Edit view</returns>
        [Authorize(Roles = "Administrator,Reporter")]
        public ActionResult Edit(int id)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Participant with id {0} edit (GET)...", id);
            Participant participant = Repository.Get(id);
            
            if (participant == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Participant with id={0} not found", id);
                return View("NotFound");
            }            
            
            var viewModel = new ParticipantManagerViewModel
            {
                Participant = participant,
                Genders = Repository.ListGenders(),
                ProfessionalBackgrounds = Repository.ListProfessionalBackgrounds(),
                Specialities = Repository.ListSpecialities()
            };

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Participant with id {0} edit (GET)...", id); 
            return View(viewModel);
        }

        //
        // POST: /TargetArea/Edit/5

        /// <summary>
        /// Edits the specified entity.
        /// </summary>
        /// <param name="id">The id of the existing entity.</param>
        /// <param name="collection">The collection of values entered.</param>
        /// <returns>Details view</returns>
        [HttpPost]
        [Authorize(Roles = "Administrator,Reporter")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Participant with id {0} edit (POST)...", id);
            Participant participant = Repository.Get(id);

            participant.Gender = Repository.GetGenderByID(int.Parse(collection["Participant.ParticipantGenderID"]));
            participant.ProfessionalBackground = Repository.GetProfessionalBackgroundByID(int.Parse(collection["Participant.ParticipantProfessionalBackgroundID"]));
            participant.Speciality = Repository.GetSpecialityByID(int.Parse(collection["Participant.ParticipantSpecialityID"]));

            TryUpdateModel(participant, "Participant");

            if (ModelState.IsValid)
            {

                Repository.Save();

                if (log.IsDebugEnabled) log.DebugFormat("Leaving Participant with id {0} edit (POST)...", id);
                return RedirectToAction("Details", "Seminar", new { id = participant.Seminar.SeminarID });
            }
            else
            {
                var viewModel = new ParticipantManagerViewModel
                {
                    Participant = participant,
                    Genders = Repository.ListGenders(),
                    ProfessionalBackgrounds = Repository.ListProfessionalBackgrounds(),
                    Specialities = Repository.ListSpecialities()
                };

                return View(viewModel);
            }
        }

        #endregion

        #region Delete

        //
        // GET: /TargetArea/Delete/5

        /// <summary>
        /// Returns the view associated with a given entity to be deleted
        /// </summary>
        /// <param name="id">The id of the existing entity</param>
        /// <returns>Delete view</returns>
        [Authorize(Roles = "Administrator,Reporter")]
        public ActionResult Delete(int id)
        {
            throw new NotImplementedException();

            if (log.IsDebugEnabled) log.DebugFormat("Entering Participant with id {0} delete (GET)...", id);
            Participant participant = Repository.Get(id);

            if (participant == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Participant with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Participant with id {0} delete (GET)...", id);
            return View(participant);
        }

        //
        // POST: /TargetArea/Delete/5

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="id">The id of the existing entity</param>
        /// <param name="confirmButton">The confirm button.</param>
        /// <returns>Deleted view</returns>
        [HttpPost]
        [Authorize(Roles = "Administrator,Reporter")]
        public ActionResult Delete(int id, string confirmButton)
        {
            throw new NotImplementedException();

            if (log.IsDebugEnabled) log.DebugFormat("Entering Participant with id {0} delete (POST)...", id);
            Participant participant = Repository.Get(id);
            
            if (participant == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Participant with id={0} not found", id);
                return View("NotFound");
            }

            int seminarID = participant.Seminar.SeminarID;

            Repository.Delete(participant);
            Repository.Save();

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Participant with id {0} delete (POST)...", id);
            return RedirectToAction("Details", "Seminar", new { id = seminarID });
        }

        #endregion

    }
}
